//
//  TTToCollectionView.swift
//  WeiBo
//
//  Created by 涂永江 on 2017/6/13.
//  Copyright © 2017年 涂永江. All rights reserved.
//

import UIKit

class TTToCollectionView: UICollectionView {

    override init(frame: CGRect, collectionViewLayout layout: UICollectionViewLayout) {
        let flowlayout = UICollectionViewFlowLayout()
        //cell之间的间隙
        let cellMagin:CGFloat = 5
        //每一个cell的宽度
        let cellWidth = (screnssWidth - 20 - 2*cellMagin)/3
        
        flowlayout.itemSize = CGSize(width: cellWidth, height: cellWidth)
        flowlayout.minimumLineSpacing = cellMagin
        flowlayout.minimumInteritemSpacing = cellMagin
        
        super.init(frame: frame, collectionViewLayout: flowlayout)
        
        setupUI()
    }
     var imageArray = [UIImage]()
    
     var addPicColse:(()->())?
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    func setupUI()
    {
    register(TTCollectionViewCell.self, forCellWithReuseIdentifier: ttoCellIdefier)
    dataSource = self
    delegate = self
    }
    
    func show(image:UIImage)
    {
        
       self.isHidden = false
     imageArray.append(image)
        
        reloadData()
    
    }
    
}
extension TTToCollectionView:UICollectionViewDataSource,UICollectionViewDelegate
{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        if imageArray.count == 0 || imageArray.count == 9{
        
            return imageArray.count
        }
        
            return imageArray.count + 1
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell  = dequeueReusableCell(withReuseIdentifier: ttoCellIdefier, for: indexPath) as! TTCollectionViewCell
        
        if indexPath.row == imageArray.count
        {
        
            cell.contentImage = nil
        }else{
        
            cell.contentImage = self.imageArray[indexPath.item]
        }
        cell.indexa = indexPath
        cell.btnColse = {[unowned self] (index)->() in
         
            let idex = index.row
            self.imageArray.remove(at: idex)
            self.reloadData()
            if self.imageArray.count == 0
            {
            
            self.isHidden = true
            }
            
        }
        
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        if indexPath.item == self.imageArray.count
        {
           addPicColse?()
        
        }
    }
    

}

class TTCollectionViewCell: UICollectionViewCell {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK:  --懒加载视图
    private lazy var picImage = UIImageView()
    var indexa:IndexPath?
    var btnColse:((IndexPath)->())?
    private lazy var picButton:UIButton = {
    
        let button = UIButton()
        button.setImage(UIImage(named:"compose_photo_close"), for: .normal)
        button.addTarget(self, action: #selector(btnAction), for: .touchUpInside)
        return button
    }()
    
    var contentImage:UIImage?
    {
        didSet{
            if contentImage == nil{
            picImage.image = UIImage(named: "compose_pic_add")
            picButton.isHidden = true
            }else{
            picButton.isHidden = false
            picImage.image = contentImage
            }
        }
    
    }
    
    func setUI()
    {
    contentView.addSubview(picImage)
    contentView.addSubview(picButton)
     picImage.snp.makeConstraints { (make) in
        make.edges.equalTo(contentView)
        }
        picButton.snp.makeConstraints { (make) in
            make.top.equalTo(picImage)
            make.trailing.equalTo(picImage)
        }
    }
    
  @objc private func btnAction()
    {
    
    btnColse?(indexa!)
    }
    
}
